package 热题100;

import java.util.ArrayList;
import java.util.List;

public class 热题17电话号码的字母组合 {

    public static void main(String[] args) {
        String s = "";
        热题17电话号码的字母组合 o = new 热题17电话号码的字母组合();
        List<String> list = o.letterCombinations(s);
        System.out.println(list);
    }

    static char[][] ch =
            {
                    {},
                    {},
                    {'a','b','c'},
                    {'d','e','f'},
                    {'g','h','i'},
                    {'j','k','l'},
                    {'m','n','o'},
                    {'p','q','r','s'},
                    {'t','u','v'},
                    {'w','x','y','z'},
            };
    List<String> resList;

    
    public List<String> letterCombinations(String digits) {

        if (digits == null || digits.length() == 0){
            return new ArrayList<>();
        }

        //将mao集合保存完毕，后，根据字符串，遍历结果
        resList = new ArrayList<>();
        //将需要转换的字符串按照层序遍历
        StringBuilder sb = new StringBuilder();
        help(digits,0,0,sb);
        return resList;
    }


    private void help(String digits, int index,int j, StringBuilder sb) {
        //如果len的长度等于digits的长度，将现在list保存在结果中
        if (sb.length() == digits.length()){
            resList.add(sb.toString());//比如23，将def中的d遍历结束以后，将d删除，准备接收f
            return;
        }

        //234，从2开始遍历
        int no = Integer.parseInt(digits.substring(index,index+1));
        for (int i = 0; i < ch[no].length; i++) {
            int i1 = no;
            sb.append(ch[i1][i]);
            //然后添加3和2的动作一致
            help(digits,index+1,0,sb);

            //最后返回的时候，将list最后一个元素删除
            sb.delete(sb.length()-1,sb.length());
            //开始遍历第二轮，
        }
    }


}
